package com.intellij.lang.jsgraphql.schema.library.impl

import com.intellij.platform.workspace.storage.WorkspaceEntityInternalApi
import com.intellij.platform.workspace.storage.metadata.impl.MetadataStorageBase
import com.intellij.platform.workspace.storage.metadata.model.EntityMetadata
import com.intellij.platform.workspace.storage.metadata.model.FinalClassMetadata
import com.intellij.platform.workspace.storage.metadata.model.OwnPropertyMetadata
import com.intellij.platform.workspace.storage.metadata.model.StorageTypeMetadata
import com.intellij.platform.workspace.storage.metadata.model.ValueTypeMetadata

@OptIn(WorkspaceEntityInternalApi::class)
internal object MetadataStorageImpl : MetadataStorageBase() {
  override fun initializeMetadata() {
    val primitiveTypeStringNotNullable = ValueTypeMetadata.SimpleType.PrimitiveType(isNullable = false, type = "String")
    val primitiveTypeStringNullable = ValueTypeMetadata.SimpleType.PrimitiveType(isNullable = true, type = "String")
    val primitiveTypeSetNotNullable = ValueTypeMetadata.SimpleType.PrimitiveType(isNullable = false, type = "Set")

    var typeMetadata: StorageTypeMetadata

    typeMetadata = FinalClassMetadata.ObjectMetadata(fqName = "com.intellij.lang.jsgraphql.schema.library.GraphQLLibraryEntitySource",
                                                     properties = listOf(
                                                       OwnPropertyMetadata(isComputable = false, isKey = false, isOpen = false,
                                                                           name = "virtualFileUrl",
                                                                           valueType = ValueTypeMetadata.SimpleType.CustomType(
                                                                             isNullable = true,
                                                                             typeMetadata = FinalClassMetadata.KnownClass(
                                                                               fqName = "com.intellij.platform.workspace.storage.url.VirtualFileUrl")),
                                                                           withDefault = false)),
                                                     supertypes = listOf("com.intellij.platform.workspace.storage.EntitySource"))

    addMetadata(typeMetadata)

    typeMetadata = EntityMetadata(fqName = "com.intellij.lang.jsgraphql.schema.library.GraphQLLibraryEntity",
                                  entityDataFqName = "com.intellij.lang.jsgraphql.schema.library.impl.GraphQLLibraryEntityData",
                                  supertypes = listOf("com.intellij.platform.workspace.storage.WorkspaceEntity"), properties = listOf(
      OwnPropertyMetadata(isComputable = false, isKey = false, isOpen = false, name = "entitySource",
                          valueType = ValueTypeMetadata.SimpleType.CustomType(isNullable = false,
                                                                              typeMetadata = FinalClassMetadata.KnownClass(
                                                                                fqName = "com.intellij.platform.workspace.storage.EntitySource")),
                          withDefault = false),
      OwnPropertyMetadata(isComputable = false, isKey = false, isOpen = false, name = "identifier",
                          valueType = primitiveTypeStringNotNullable, withDefault = false),
      OwnPropertyMetadata(isComputable = false, isKey = false, isOpen = false, name = "displayName",
                          valueType = primitiveTypeStringNotNullable, withDefault = false),
      OwnPropertyMetadata(isComputable = false, isKey = false, isOpen = false, name = "description",
                          valueType = primitiveTypeStringNullable, withDefault = false),
      OwnPropertyMetadata(isComputable = false, isKey = false, isOpen = false, name = "attachmentScope",
                          valueType = ValueTypeMetadata.SimpleType.CustomType(isNullable = false,
                                                                              typeMetadata = FinalClassMetadata.EnumClassMetadata(
                                                                                fqName = "com.intellij.lang.jsgraphql.schema.library.GraphQLLibraryAttachmentScope",
                                                                                properties = listOf(),
                                                                                supertypes = listOf("java.io.Serializable",
                                                                                                    "kotlin.Comparable",
                                                                                                    "kotlin.Enum"),
                                                                                values = listOf("GLOBAL",
                                                                                                "PROJECT"))), withDefault = true),
      OwnPropertyMetadata(isComputable = false, isKey = false, isOpen = false, name = "roots",
                          valueType = ValueTypeMetadata.ParameterizedType(generics = listOf(
                            ValueTypeMetadata.SimpleType.CustomType(isNullable = false, typeMetadata = FinalClassMetadata.KnownClass(
                              fqName = "com.intellij.platform.workspace.storage.url.VirtualFileUrl"))),
                                                                          primitive = primitiveTypeSetNotNullable), withDefault = false)),
                                  extProperties = listOf(), isAbstract = false)

    addMetadata(typeMetadata)
  }

  override fun initializeMetadataHash() {
    addMetadataHash(typeFqn = "com.intellij.lang.jsgraphql.schema.library.GraphQLLibraryEntity", metadataHash = -1849614646)
    addMetadataHash(typeFqn = "com.intellij.lang.jsgraphql.schema.library.GraphQLLibraryAttachmentScope", metadataHash = -912984699)
    addMetadataHash(typeFqn = "com.intellij.platform.workspace.storage.EntitySource", metadataHash = 212848188)
    addMetadataHash(typeFqn = "com.intellij.lang.jsgraphql.schema.library.GraphQLLibraryEntitySource", metadataHash = 619168016)
  }

}
